package com.rxmvp.subscribers;

import android.util.Log;

import com.rxmvp.http.exception.ApiException;
import com.rxmvp.utils.NetworkUtil;

/**
 * 后台静默访问的返回
 */
public abstract class CommonSubscriber<T> extends BaseSubscriber<T> {
    public CommonSubscriber() {

    }

    private static final String TAG = CommonSubscriber.class.getSimpleName();

    @Override
    public void onStart() {
        super.onStart();

        // if  NetworkAvailable no !   must to call onCompleted
        if (!NetworkUtil.isNetworkAvailable()) {
            onCompleted();
            // 无网络执行complete后取消注册防调用onError
            if (!isUnsubscribed()) {
                unsubscribe();
            }
        } else {
            Log.d(TAG, "network available");
        }
    }

    @Override
    public void onCompleted() {
        Log.d(TAG, "onCompleted~ ");
    }

    @Override
    protected void onError(ApiException ex) {
        Log.e(TAG, "onError: " + ex.message + "code: " + ex.code);
        //Toast.makeText(mContext, ex.message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public abstract void onNext(T t);
}
